#ifndef _ITEM_DATA_HPP_
#define _ITEM_DATA_HPP_

#include "item.hpp"
#include "generator.hpp"
#include "mingpp.h"

class item_data:public xlider_item {
public:
	xlider_generator* generator;
	string entry;
	
	item_data(string newname, int lev, xlider_presentation* master){
		// string ty, int lev, double wscr, double hscr, string home){
		name=newname;
		level=lev;
		home_path=master->home_path;
		slide_width=master->slide_width;
		slide_height=master->slide_height;
		margin_left=master->margin_left;
		margin_top=master->margin_top;
		margin_right=master->margin_right;
		background_color=master->background_color;
		background_image=master->background_image;
		background_scale=master->background_scale;
		text.clear();
		height=0.0;
		width=0.0;
		x=y=offsetx=offsety=0.0;
		floating=false;
		align="left";
		bullet_radius=0.005;
		bullet_space=0.01;
		generator=NULL;
		type="text";
	}

	virtual ~item_data(){}
		
	virtual void read_theme_entry_default(xml_config_reader& xc, const string entail, string &data) {
		string fullentry(entry);fullentry.append(".");fullentry.append(entail);
		string defentry("theme.default.");defentry.append(entail);
		if(xc.Exist(fullentry.c_str()))data=xc.PeekString(fullentry.c_str());
		else data=xc.GetString(defentry.c_str());
	}

	virtual void read_theme_entry_default(xml_config_reader& xc, const string entail, double &data) {
		string fullentry(entry);fullentry.append(".");fullentry.append(entail);
		string defentry("theme.default.");defentry.append(entail);
		if(xc.Exist(fullentry.c_str())) data=xc.GetDouble(fullentry.c_str());
		else data=xc.GetDouble(defentry.c_str());
	}

	virtual void read_theme_entry(xml_config_reader& xc, const string entail, string &data, string def="") {
		string fullentry(entry);fullentry.append(".");fullentry.append(entail);
		if(xc.Exist(fullentry.c_str())) data=xc.PeekString(fullentry.c_str());
		else data=def;
	}
	
	virtual void read_theme_entry(xml_config_reader& xc, const string entail, double &data, double def=0.0) {
		string fullentry(entry);fullentry.append(".");fullentry.append(entail);
		if(xc.Exist(fullentry.c_str())){
			data=xc.GetDouble(fullentry.c_str());
		}
		else data=def;
	}
	
	virtual void read_param(param_handler& p) {
		if(p.exist("distance"))distance=p.double_value("distance");
		if(p.exist("spacing"))spacing=p.double_value("spacing");
		if(p.exist("indent"))indent=p.double_value("indent");
		if(p.exist("font"))fontname=p.string_value("font");
		if(p.exist("color"))fontcolor=p.string_value("color");
		if(p.exist("fontsize"))fontsize=p.double_value("fontsize");
		if(p.exist("align"))align=p.string_value("align");
		if(p.exist("bultype"))bullet_type=p.string_value("bultype");
		if(p.exist("bulcolor"))bullet_color=p.string_value("bulcolor");

		// ---- floating items ----
		if(p.exist("float")){
			string flt=p.string_value("float");
			if(flt=="yes") floating=true;
			if(flt=="true") floating=true;
			if(flt=="1") floating=true;
		}
		if(floating){
			if(p.exist("x"))x=p.double_value("x");
			if(p.exist("y"))y=p.double_value("y");
		}
	}
	

	virtual void read_theme(xml_config_reader& xc, param_handler& p, string ty){

		if(ty=="item"){
			char st[256];
			sprintf(st,"theme.item.%d", level);
			entry.assign(st);
		} else {
			entry.assign("theme.");
			entry.append(ty);
		}
		
		read_theme_entry_default(xc, "distance", distance);
		read_theme_entry_default(xc, "spacing", spacing);
		read_theme_entry_default(xc, "indent", indent);
		read_theme_entry_default(xc, "font.name", fontname);
		read_theme_entry_default(xc, "font.color", fontcolor);
		read_theme_entry_default(xc, "font.size", fontsize);
		read_theme_entry_default(xc, "align", align);
		// FIXME! bullet property is not complete
		read_theme_entry_default(xc, "bullet.color", bullet_color);
		read_theme_entry_default(xc, "bullet.type", bullet_type);
		
		string flt;
		floating=false;
		
		read_theme_entry(xc, "float", flt, "false");
		if(flt=="yes") floating=true;
		if(flt=="true") floating=true;
		if(flt=="1") floating=true;
		if(floating) {
			read_theme_entry(xc, "position.x", x, 0.0);
			read_theme_entry(xc, "position.y", y, 0.0);
		}

		// overide with parameter
		read_param(p);		
		
		// FIXME! must use correct number of digit. Bug: 0.7 --> 0.75

	}
	
	// all variables must be previously initialized
	void read_theme_save(xml_config_reader& xc, param_handler& p, string ty){
		if(ty=="item"){
			char st[256];
			sprintf(st,"theme.item.%d", level);
			entry.assign(st);
		} else {
			entry.assign("theme.");
			entry.append(ty);
		}
		read_theme_entry(xc, "distance", distance, distance);
		read_theme_entry(xc, "spacing", spacing, spacing);
		read_theme_entry(xc, "indent", indent, indent);
		read_theme_entry(xc, "font.name", fontname, fontname);
		read_theme_entry(xc, "font.color", fontcolor, fontcolor);
		read_theme_entry(xc, "align", align, align);
		read_theme_entry(xc, "font.size", fontsize, fontsize);		
		read_theme_entry(xc, "bullet.color", bullet_color, bullet_color);
		read_theme_entry(xc, "bullet.type", bullet_type, bullet_type);

		string flt;
		read_theme_entry(xc, "float", flt, "false");
		if(flt=="yes") floating=true;
		if(flt=="true") floating=true;
		if(flt=="1") floating=true;
		if(floating) {
			read_theme_entry(xc, "position.x", x, x);
			read_theme_entry(xc, "position.y", y, y);
		}

		read_param(p);
	}
	
	void set_generator(xlider_generator* gen){
		generator=gen;
		generator->set_item(this);
	}	
	
	// delete candidate
	virtual double draw(void* draw_param) {}

	virtual void* generate(void* param=NULL){
		assert(generator, "Generator is not assign");
//		DEBUGSTR("creating", (name+"@"+type).c_str());
		return generator->generate(param);
	}

};

#endif
